package com.vca.admin.controller;

import com.vca.common.page.CommonPage;
import com.vca.common.request.*;
import com.vca.common.response.*;
import com.vca.common.result.CommonResult;
import com.vca.service.service.StoreProductService;
import com.vca.service.service.SystemGroupDataService;
import com.vca.service.service.VcaProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * 商品表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/product")
@Api(tags = "Vca商品") //配合swagger使用
public class VcaProductController {

    @Autowired
    private VcaProductService productService;

    @Autowired
    private SystemGroupDataService systemGroupDataService;



    /**
     * 分页显示商品表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:product:list')")
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<VcaProductResponse>> getList(@Validated StoreProductSearchRequest request,
                                                                @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(productService.getAdminList(request, pageParamRequest)));
    }

    /**
     * 修改商品排序
     * @param productId
     * @param sort
     * @author Li
     * @date 2022/11/23 10:05
     * @return
     */
    @PreAuthorize("hasAuthority('admin:product:update:sort')")
    @ApiOperation(value = "修改排序")
    @GetMapping("/sort")
    public CommonResult updateSort(Integer productId, Integer sort){
        return productService.updateSort(productId, sort)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 新增商品
     * @param request 新增参数
     */
    @PreAuthorize("hasAuthority('admin:product:save')")
    @ApiOperation(value = "新增商品")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated VcaProductAddRequest request) {
        return productService.addProduct(request)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 获取礼品卡编号
     * @author Li
     * @date 2022/11/24 10:14
     * @return
     */
    @ApiOperation("获取礼品卡编号")
    @GetMapping("/getCard")
    public CommonResult getCard(){
        return CommonResult.success(systemGroupDataService.getCardList());
    }

    /**
     * 获得商品头部数据
     * @author Li
     * @date 2022/11/23 09:16
     * @return
     */
    @ApiOperation(value = "获得商品头部数据")
    @GetMapping("/header")
    public CommonResult<List<AdminHeaderResponse>> getHeader() {
        return CommonResult.success(productService.getHeader());
    }

    /**
     * 根据商品id获取商品信息
     * @param productId
     * @author Li
     * @date 2022/11/24 09:54
     * @return
     */
    @PreAuthorize("hasAuthority('admin:product:info')")
    @ApiOperation(value = "根据商品id获取商品信息")
    @GetMapping("/getProduct")
    public CommonResult<ProductAdminResponse> getProduct(Integer productId) {
        return CommonResult.success(productService.getProductAdmin(productId));
    }

    /**
     * 删除商品表
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:product:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestBody @PathVariable Integer id) {
        if (productService.deleteProduct(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 商品修改
     * @param request 商品参数
     */
    @PreAuthorize("hasAuthority('admin:product:update')")
    @ApiOperation(value = "商品修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated VcaProductAddRequest request) {
        if (productService.updatePrdouct(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 上架
     */
    @PreAuthorize("hasAuthority('admin:product:up')")
    @ApiOperation(value = "上架")
    @RequestMapping(value = "/putOnShell/{id}", method = RequestMethod.GET)
    public CommonResult<String> putOn(@PathVariable Integer id) {
        if (productService.putOnShelf(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 下架
     */
    @PreAuthorize("hasAuthority('admin:product:down')")
    @ApiOperation(value = "下架")
    @RequestMapping(value = "/offShell/{id}", method = RequestMethod.GET)
    public CommonResult<String> offShell(@PathVariable Integer id) {
        if (productService.offShelf(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


}



